﻿''==========================================
'' 文件：DeviceRestartTimeService.vb
'' 作者：李大龙
'' 日期：2011-1-06
'' 版本：v1.0
'' 描述：设备启动和停止时间数据存储

''' <summary>
''' 设备启动和停止时间数据存储
''' </summary>
''' <remarks></remarks>
Public Class DeviceRestartTimeService

    ''' <summary>
    ''' 获取设备最后一次的启动时间
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks>对于未上传的返回 1900-1-1</remarks>
    Public Function GetDeviceLastStartTime(ByVal netID As Integer, ByVal projectID As String) As DateTime
        Dim result As DateTime = New DateTime(1900, 1, 1)
        Dim strSql As String = String.Format("SELECT RestartedTime FROM tbccdevicerestarttime WHERE netID = {0} And projectID='{1}' ORDER BY RestartedTime DESC LIMIT 1", netID, projectID)
        Dim obj As Object = TBCC.Utility.SqlHelper.ExecuteScalar(strSql, Nothing)
        If (obj IsNot Nothing) Then
            result = Convert.ToDateTime(obj)
        End If
        Return result
    End Function

    ''' <summary>
    ''' 获取最后一次上传的 第零条 开始时间
    ''' </summary>
    Public Function GetDeviceLastFirstTime(ByVal netID As Integer, ByVal projectID As String) As DateTime
        Dim result As DateTime = New DateTime(1900, 1, 1)
        Dim strSql As String = String.Format("SELECT FirstTime FROM tbccdevicerestarttime WHERE netID = {0} And projectID='{1}' ORDER BY RestartedTime DESC LIMIT 1", netID, projectID)
        Dim obj As Object = TBCC.Utility.SqlHelper.ExecuteScalar(strSql, Nothing)
        If (obj IsNot Nothing) Then
            result = Convert.ToDateTime(obj)
        End If
        Return result
    End Function

    ''' <summary>
    ''' 保持记录到数据库中
    ''' </summary>
    ''' <param name="lst">要插入的数据</param>
    ''' <remarks></remarks>
    Public Sub AddRecords(ByVal lst As IList(Of DeviceRestartTime), ByVal projectID As String)
        If (lst.Count = 0) Then Exit Sub

        Dim strSql As String = "INSERT INTO tbccdevicerestarttime (projectID,netID,IsNew,LastTime,RestartedTime,FirstTime) values "
        Dim sb As New System.Text.StringBuilder()
        Dim drt As DeviceRestartTime
        For index As Integer = 0 To lst.Count - 1
            drt = lst(index)
            If (drt.IsNew = 0) Then
                sb.AppendFormat("('{0}',{1},0,'{2}','{3}','{4}'),", projectID, drt.NetID, drt.LastTime.ToString("yyyy-MM-dd HH:mm:ss"), drt.RestartedTime.ToString("yyyy-MM-dd HH:mm:ss"), drt.FirstTime.ToString("yyyy-MM-dd HH:mm:ss"))
            Else
                sb.AppendFormat("('{0}',{1},1,'{2}','{3}','{4}'),", projectID, drt.NetID, drt.LastTime.ToString("yyyy-MM-dd HH:mm:ss"), drt.RestartedTime.ToString("yyyy-MM-dd HH:mm:ss"), drt.FirstTime.ToString("yyyy-MM-dd HH:mm:ss"))
            End If
        Next
        sb = sb.Remove(sb.Length - 1, 1)
        sb.Append(";")
        strSql = strSql + sb.ToString()

        TBCC.Utility.SqlHelper.ExecuteNonQuery(strSql, Nothing)
    End Sub

    ''' <summary>
    ''' 根据条件查询数据
    ''' </summary>
    ''' <param name="strFilter">过滤条件</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Query(ByVal strFilter As String) As IList(Of DeviceRestartTime)
        Dim lst As New List(Of DeviceRestartTime)
        Dim strSql As String = "SELECT netID,IsNew,LastTime,RestartedTime,FirstTime FROM tbccdevicerestarttime "
        If Not (String.IsNullOrEmpty(strFilter)) Then
            strSql = strSql + " Where " + strFilter
        End If

        strSql += " Order by netID,RestartedTime"
        Dim ds As DataSet
        ds = TBCC.Utility.SqlHelper.ExecuteDataset(strSql, Nothing)
        If (ds IsNot Nothing) Then
            If (ds.Tables.Count > 0) Then
                Dim table As DataTable = ds.Tables(0)
                If table.Rows.Count > 0 Then
                    Dim drt As DeviceRestartTime
                    Dim row As DataRow

                    For i As Integer = 0 To table.Rows.Count - 1
                        drt = New DeviceRestartTime()


                        row = table.Rows(i)
                        drt.NetID = Convert.ToInt32(row("netID").ToString())
                        drt.Name = String.Format("{0}#设备", drt.NetID)
                        drt.IsNew = Convert.ToInt32(row("IsNew").ToString())
                        drt.LastTime = Convert.ToDateTime(row("LastTime").ToString())
                        drt.RestartedTime = Convert.ToDateTime(row("RestartedTime").ToString())
                        drt.FirstTime = Convert.ToDateTime(row("FirstTime").ToString())
                        lst.Add(drt)
                    Next
                End If
            End If
        End If
        Return lst
    End Function

    ''' <summary>
    '''  获取设备列表
    ''' </summary>
    Public Function GeDevices(ByVal projectId As String) As IList(Of DevTypeItem)
        Dim il As IList(Of DevTypeItem)
        il = New List(Of DevTypeItem)

        Dim ds As DataSet

        Dim strSql As String
        strSql = String.Format("Select * From tbccDevType where DevType=1  And ProjectID='{0}' order by projectID, Netid", projectId)

        ds = TBCC.Utility.SqlHelper.ExecuteDataset(strSql, Nothing)

        For Each r As DataRow In ds.Tables(0).Rows
            Dim dti As New DevTypeItem()
            dti.ProjectID = r("projectID")
            dti.NetId = r("Netid")
            dti.DevType = r("DevType")
            dti.RefNum = r("RefNum")
            dti.DevAppType = r("DevAppType")

            il.Add(dti)
        Next

        Return il

    End Function

End Class
